<?php
namespace app\admin\controller;
require '../vendor/autoload.php';

use think\Db;
use think\Controller;
class GoodsSpecificationValue extends Controller{
    public function index(){
//        查询所有的规格值
        $re_goods_specification_value = Db::table('goods_specification_value')->select();
//        var_dump($re_goods_specification_value);exit;
//        //查询商品类型
        if(!empty($re_goods_specification_value)){
            foreach($re_goods_specification_value as $k=>$v){
                //追加分类名称
                $category = Db::table('goods_category','id ='.$v['cid'])->find();
                $category = Db::table('goods_category')->where(array('id'=>$v['cid']))->find();
                $re_goods_specification_value[$k]['category_name'] = $category['name'];
                //追加规格名
                $goods_specification = Db::table('goods_specification')->where(array('id'=>$v['sid']))->find();
                $re_goods_specification_value[$k]['s_name'] = $goods_specification['name'];
            }
        }
//        var_dump($re_goods_specification_value);exit;
        $this->assign('re_goods_specification_value',$re_goods_specification_value);
        return view('index');
    }
    public function edit_Specification(){
        $cid = $_POST['cid'];
        $name = $_POST['name'];
        $status = $_POST['status'];
        $create_time = time();
        $data=array(
            'cid'=>$cid,
            'name'=>$name,
            'status'=>$status,
            'create_time'=>$create_time
        );
        $res = Db::table('goods_specification')->insert($data);
        if($res){
            $this->success('新增成功', 'index');
        }
        return view('GoodsSpecification/index');
    }
    //添加
    public function add(){
        if($_POST){ //提交表单
            $data=array(
                'cid'=>$_POST['cid'],
                'sid'=>$_POST['s_name'],
                'value'=>$_POST['s_value'],
                'create_time'=>time(),
                'status'=>$_POST['status'],
            );
            $res = Db::table('goods_specification_value')->insert($data);
            if($res){
                $this->success('新增成功', 'index');
            }
        }else{
            $GoodsCategory = Db::table('goods_category')->where(array('status'=>1))->select();
            $this->assign('nodes',json_encode($GoodsCategory));
            return view('edit');
        }
    }
    //获取下拉的规格名
    public function getname(){
        $id = $_POST['id'];
        if($id==0){
//            $this->ajaxReturn(0);
            echo json_encode(array('error_code' => 0, 'error_string' => '', 'datas' =>''));exit;
        }else{
            $re = Db::table('goods_category')->where(array("id"=>$id))->find();
            //判断是否是顶级分类
            $num = Db::table('goods_category')->where(array('parent_id'=>$re['id']))->select();
            if($num){
                echo json_encode(array('error_code' => 0, 'error_string' => '', 'datas' => ''));exit;
            }else{
                //查询规格名
                $re_name = Db::table('goods_specification')->where(array('cid'=>$re['id']))->select();
                if(empty($re_name)){
                    echo json_encode(-1);
                }else{
//                    echo json_encode($re_name);
                    echo json_encode(array('error_code' => 1, 'error_string' => '', 'datas' => $re_name));exit;
                }
            }
        }
    }
}